<template>
	<d2-container>
		<!-- 搜索 -->
		<template slot="header">
			<el-form ref="queryForm" :model="queryForm" class="header-area" label-width="80px" style="height:auto">
				<el-row>
					<el-col :span="5">
						<el-form-item label="专题标题" prop="title">
							<el-input type="text" v-model="queryForm.title" placeholder="请输入标题关键字"></el-input>
						</el-form-item>
					</el-col>
					
					<el-col :span="5">
						<el-form-item label="发布状态" prop="publishStatus">
							<el-select v-model="queryForm.publishStatus" placeholder="请选择" style="width: 100%;">
								<el-option label="全部" value=""></el-option>
								<el-option label="下架" value="0"></el-option>
								<el-option label="上架" value="1"></el-option>
							</el-select>
						</el-form-item>
					</el-col>
					
					<el-col :span="4">
						<div class="search-submit">
							<el-button type="primary" @click="submitForm('queryForm')">查询</el-button>
							<el-button @click="resetForm('queryForm')">重置</el-button>
						</div>
					</el-col>
				</el-row>
			</el-form>
		</template>
		
		<!-- 列表部分 -->
		<template>
			<!-- 表格上方的按钮 -->
			<div class="table-header">
				<el-button type="primary" @click="add">添加新专题</el-button>
			</div>
			
			<el-table :data="tableData" stripe border style="width: 100%">
				<el-table-column type="index" fixed label="序号" width="80" align="center"></el-table-column>
				
				<el-table-column prop="skuThumbImage" label="页面头图" width="180" align="center">
					<template slot-scope="scope">
						<el-image style="width: 80px; height: 35px" :src="scope.row.posterUrl" fit="cover" :preview-src-list="[scope.row.posterUrl]"></el-image>
					</template>
				</el-table-column>
				
				<el-table-column prop="title" label="标题"></el-table-column>
				<el-table-column prop="sort" label="排序" width="150" align="center"></el-table-column>
				
				<el-table-column label="开始时间" :formatter="forDate" prop="startTime" width="180" align="center"></el-table-column>
				<el-table-column label="结束时间" :formatter="forDate" prop="endTime" width="180" align="center"></el-table-column>
				
				<el-table-column label="发布状态" prop="publishStatus" width="160" align="center">
					<template slot-scope="scope">
						<el-tag type="success" v-if="scope.row.publishStatus == 1">上架中</el-tag>
						<el-tag type="warning" v-if="scope.row.publishStatus == 0">已下架</el-tag>
					</template>
				</el-table-column>
				
				<el-table-column label="操作" width="220" align="center" fixed="right">
					<template slot-scope="scope">
						<!-- <el-button type="text" size="mini" icon="el-icon-zoom-in" @click="viewDetail(scope.row)">查看详情</el-button> -->
						<el-button type="text" size="mini" icon="el-icon-circle-plus-outline" @click="editProduct(scope.row)">设置专题商品</el-button>
						<el-button type="text" size="mini" icon="el-icon-edit" @click="edit(scope.row)">编辑信息</el-button>
					</template>
				</el-table-column>
			</el-table>
		</template>
		
		<!-- 翻页 -->
		<template slot="footer">
			<div class="page">
				<el-pagination
					background
					@size-change="handleSizeChange"
					@current-change="handleCurrentChange"
					:current-page="currentPage"
					:page-sizes="[10, 20, 50, 100]"
					:page-size="pageSize"
					layout="total, sizes, prev, pager, next, jumper"
					:total="total"
				></el-pagination>
			</div>
		</template>
		
		<template>
			<!-- 添加/编辑专题页弹窗 -->
			<handle-special-page-dialog
				:isShow="showHandleDialog"
				:item="currentItem"
				:isEdit="isEdit"
				@submit="fetchData"
				@close="closeHandleDialog"
			></handle-special-page-dialog>
			
			<!-- 设置专题页商品弹窗 -->
			<handle-special-page-product-dialog
				:isShow="showHandleProductDialog"
				:item="currentItem"
				@close="closeHandleProductDialog"
			></handle-special-page-product-dialog>
		</template>
	</d2-container>
</template>

<script>
	import dayjs from 'dayjs'
	import { getSpecialPageList } from '@api/platform/platformMarketing'
	import handleSpecialPageDialog from './components/handle-special-page-dialog'
	import handleSpecialPageProductDialog from './components/handle-special-page-product-dialog'
	export default {
		components: {
			handleSpecialPageDialog,
			handleSpecialPageProductDialog,
		},
		data() {
			return {
				queryForm: {
					title: '',
					publishStatus: '',
				},
				time: [],
				pickerOptions: {
					shortcuts: [{
							text: '最近一周',
							onClick(picker) {
								const end = new Date()
								const start = new Date()
								start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
								picker.$emit('pick', [start, end])
							}
						},
						{
							text: '最近一个月',
							onClick(picker) {
								const end = new Date()
								const start = new Date()
								start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
								picker.$emit('pick', [start, end])
							}
						},
						{
							text: '最近三个月',
							onClick(picker) {
								const end = new Date()
								const start = new Date()
								start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
								picker.$emit('pick', [start, end])
							}
						}
					]
				},
				
				tableData: [],
				currentPage: 1,
				pageSize: 10,
				total: 0,
				
				showHandleDialog: false, //添加修改弹窗
				showHandleProductDialog: false, //设置商品弹窗
				currentItem: {},
				isEdit: false,
			}
		},
		methods: {
			fetchData() {
				if(this.loading) {
					return;
				}
				this.loading = true;
				
				getSpecialPageList({
					title: this.queryForm.title,
					publishStatus: this.queryForm.publishStatus,
					currentPage: this.currentPage,
					pageSize: this.pageSize
				})
					.then(res => {
						this.tableData = res.data.content;
						this.total = res.data.total;
						this.loading = false;
					})
					.catch(err => {
						console.log('err', err);
						this.loading = false;
					})
			},
			handleSizeChange(val) {
				this.pageSize = val;
				this.fetchData();
			},
			handleCurrentChange(val) {
				this.currentPage = val;
				this.fetchData();
			},
			resetPage() {
				this.currentPage = 1;
				this.pageSize = 10;
				this.total = 0;
			},
			forDate(row, column) { //时间格式化
				if(column.property == 'startTime') {
					return row.startTime == null ? '-' : dayjs(row.startTime).format('YYYY-MM-DD HH:mm:ss');
				}
				if(column.property == 'endTime') {
					return row.endTime == null ? '-' : dayjs(row.endTime).format('YYYY-MM-DD HH:mm:ss');
				}
			},
			
			submitForm(formName) {
				this.resetPage();
				this.fetchData();
			},
			resetForm(formName) {
				this.$refs[formName].resetFields();
				this.time = [];
				this.resetPage();
				this.fetchData();
			},
			
			add() { //添加信息
				this.showHandleDialog = true;
			},
			edit(item) { //编辑信息
				this.currentItem = item;
				this.isEdit = true;
				this.showHandleDialog = true;
			},
			closeHandleDialog() {
				this.showHandleDialog = false;
			},
			
			editProduct(item) { //设置商品
				this.currentItem = item;
				this.showHandleProductDialog = true;
			},
			closeHandleProductDialog() {
				this.showHandleProductDialog = false;
			}
		},
		mounted() {
			this.fetchData();
		}
	}
</script>

<style>
</style>